package com.weitu.contractor.service.impl;

import com.weitu.contractor.entity.ContractorAudit;
import com.weitu.contractor.entity.ContractorExposure;
import com.weitu.contractor.entity.ContractorExposureCriteria;
import com.weitu.contractor.mapper.ContractorAuditMapper;
import com.weitu.contractor.mapper.ContractorExposureMapper;
import com.weitu.contractor.service.ContractorExposureService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ContractorExposureServiceImpl implements ContractorExposureService {
    private final ContractorExposureMapper contractorExposureMapper;
    private final ContractorAuditMapper contractorAuditMapper;

    private static final Logger logger = LoggerFactory.getLogger(ContractorExposureServiceImpl.class);

    @Autowired
    public ContractorExposureServiceImpl(ContractorExposureMapper contractorExposureMapper, ContractorAuditMapper contractorAuditMapper) {
        this.contractorExposureMapper = contractorExposureMapper;
        this.contractorAuditMapper = contractorAuditMapper;
    }

    @Override
    public int countByCriteria(ContractorExposureCriteria example) {
        int count = this.contractorExposureMapper.countByCriteria(example);
        logger.debug("count: {}", count);
        return count;
    }

    @Override
    public ContractorExposure selectByPrimaryKey(String id) {
        return this.contractorExposureMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ContractorExposure> selectByCriteria(ContractorExposureCriteria example) {
        return this.contractorExposureMapper.selectByCriteria(example);
    }

    @Override
    @Transactional
    public int deleteByPrimaryKey(String id) {
        return this.contractorExposureMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int updateByPrimaryKeySelective(ContractorExposure record) {
        return this.contractorExposureMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    @Transactional
    public int updateByCriteriaSelective(ContractorExposure record, ContractorExposureCriteria example) {
        return this.contractorExposureMapper.updateByCriteriaSelective(record, example);
    }

    @Override
    @Transactional
    public int insertSelective(ContractorExposure record) {
        return this.contractorExposureMapper.insertSelective(record);
    }

    @Override
    public boolean checkIfExist(String contractorAuditId, String exposureEndDate) {
        ContractorExposureCriteria exposureCriteria = new ContractorExposureCriteria();
        ContractorExposureCriteria.Criteria criteria = exposureCriteria.createCriteria();
        criteria.andDelStateEqualTo(0);
        if (StringUtils.isNotEmpty(exposureEndDate)){
            criteria.andExposureEndDateGreaterThanOrEqualTo(exposureEndDate);
        }
        List<ContractorExposure> contractorExposures = this.contractorExposureMapper.selectByCriteria(exposureCriteria);
        ContractorAudit contractorAudit = this.contractorAuditMapper.selectByPrimaryKey(contractorAuditId);
        for (ContractorExposure contractorExposure : contractorExposures) {
            if (contractorExposure.getContractorAudit().getContractorId().equals(contractorAudit.getContractorId())){
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean checkIfExist(String contractorAuditId, String exposureEndDate, String id) {
        ContractorExposureCriteria exposureCriteria = new ContractorExposureCriteria();
        ContractorExposureCriteria.Criteria criteria = exposureCriteria.createCriteria();
        criteria.andDelStateEqualTo(0).andIdNotEqualTo(id);
        if (StringUtils.isNotEmpty(exposureEndDate)){
            criteria.andExposureEndDateGreaterThanOrEqualTo(exposureEndDate);
        }
        List<ContractorExposure> contractorExposures = this.contractorExposureMapper.selectByCriteria(exposureCriteria);
        ContractorAudit contractorAudit = this.contractorAuditMapper.selectByPrimaryKey(contractorAuditId);
        for (ContractorExposure contractorExposure : contractorExposures) {
            if (contractorExposure.getContractorAudit().getContractorId().equals(contractorAudit.getContractorId())){
                return true;
            }
        }
        return false;
    }
}